//
//  AllWarnViewController.m
//  Rail
//
//  Created by hychen on 2018/2/28.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "AllWarnViewController.h"
#import "AllWarnHeaderView.h"
#import "AllWarnEventTableViewCell.h"
#import "AllWarnEventSectionView.h"
#import "WarnBaseViewController.h"
#import "TodoWarnViewController.h"
@interface AllWarnViewController ()
@property (nonatomic, strong) InformationAlertModel *alertModel;
@property (nonatomic, strong) AllWarnHeaderView *headerView;
@end

@implementation AllWarnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"全部报警"];
    [self prepareTableHeaderView];
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareTableHeaderView {
    BMWeakSelf
    AllWarnHeaderView *headerView = [AllWarnHeaderView headerView];
    
    self.headerView = headerView;
    //赋值
    self.headerView.model = self.alertModel;
    self.tableView.tableHeaderView = headerView;
    
    [headerView setBlockClickTodo:^{
        WarnBaseViewController *vc = [WarnBaseViewController new];
        vc.type = WarnRequestDataTypeTodo;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    [headerView setBlockClickFinish:^{
        WarnBaseViewController *vc = [WarnBaseViewController new];
        vc.type = WarnRequestDataTypeDone;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    [headerView setBlockClickNormal:^{
        WarnBaseViewController *vc = [WarnBaseViewController new];
        vc.type = WarnRequestDataTypeNormal;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    [headerView setBlockClickWarn:^{
        WarnBaseViewController *vc = [WarnBaseViewController new];
        vc.type = WarnRequestDataTypeWarn;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    [headerView setBlockClickSerivous:^{
        WarnBaseViewController *vc = [WarnBaseViewController new];
        vc.type = WarnRequestDataTypeSerivous;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    [headerView setBlockClickHeavy:^{
        WarnBaseViewController *vc = [WarnBaseViewController new];
        vc.type = WarnRequestDataTypeHeavy;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)refreshData {
    BMWeakSelf
    [HttpRequest loadInformationWithAlertStatisticSuccess:^(InformationAlertModel *object) {
        weakSelf.alertModel = object;
        weakSelf.dataArray = object.event_list.mutableCopy;
        [weakSelf prepareTableHeaderView];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        Show_SV_Error_Msg(msg)
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [AllWarnEventSectionView sectionView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AllWarnEventTableViewCell *cell = [AllWarnEventTableViewCell cellWithTableView:tableView class:[AllWarnEventTableViewCell class]];
    InformationEventModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    InformationEventModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    //跳转
    TodoWarnViewController *vc = [TodoWarnViewController new];
    vc.eventModel = model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.f;
}
@end
